package com.softeem.idriveserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.idriveserver.dto.Result;
import com.softeem.idriveserver.entity.VehicleInfo;
import com.softeem.idriveserver.service.VehicleInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆信息接口
 */
@Tag(name = "车辆管理")
@RestController
public class VehicleInfoController {

    @Autowired
    private VehicleInfoService vehicleInfoService;

    // 添加车辆
    @Operation(summary = "车辆新增",description = "需要提供车辆信息")
    @PostMapping("/vehicle")
    public Result add(@RequestBody VehicleInfo vehicleInfo){
        System.out.println(vehicleInfo);
        //根据VIN号查询汽车是否存在
        //不存在则执行添加 
        return null;
    }

    // 查询车辆信息（分页）
    @Operation(summary = "车辆列表",description = "分页查询车辆列表")
    @Parameters({
            @Parameter(name = "page",description = "页码",required = false),
            @Parameter(name = "limit",description = "每页数据条数",required = false)
    })
    @GetMapping("/vehicle/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        List<VehicleInfo> list = vehicleInfoService.list(new Page<VehicleInfo>(page, limit));
        long count = vehicleInfoService.count();
        return Result.ok().setData(list).setMsg("查询成功").setCount(count);
    }

    // 根据汽车vin号查询车辆信息
    @Operation(summary = "根据汽车vin号查询车辆信息",description = "根据汽车vin号查询车辆信息")
    @GetMapping("/vehicle/vin/{vin}")
    public Result queryByVin(@PathVariable String vin){
        VehicleInfo vehicleInfo = vehicleInfoService.getOne(Wrappers.lambdaQuery(VehicleInfo.class).eq(VehicleInfo::getVin,vin));
        if(vehicleInfo == null){
            return Result.fail().setMsg("该汽车不存在");
        }
        return Result.ok().setData(vehicleInfo);
    }

    // 根据汽车id查询车辆信息
    @Operation(summary = "根据汽车id查询车辆信息",description = "根据汽车id查询车辆信息")
    @GetMapping("/vehicle/{id}")
    public Result queryById(@PathVariable Integer id){
        VehicleInfo vehicleInfo = vehicleInfoService.getById(id);
        if(vehicleInfo == null){
            return Result.fail().setMsg("该汽车不存在");
        }
        return Result.ok().setData(vehicleInfo);
    }

    // 修改车辆信息
    @Operation(summary = "修改车辆信息",description = "修改车辆信息")
    @PutMapping("/vehicle")
    public Result modify(@RequestBody VehicleInfo vehicleInfo){
        return vehicleInfoService.updateById(vehicleInfo) ? Result.ok().setMsg("修改成功") : Result.fail().setMsg("修改失败");
    }

    // 删除车辆信息
    @Operation(summary = "删除车辆",description = "根据id删除车辆")
    @DeleteMapping("/vehicle/{id}")
    public Result delete(@PathVariable Integer id){
        //若设计到级联操作需要单独处理
        return vehicleInfoService.removeById(id) ? Result.ok().setMsg("删除成功") : Result.fail().setMsg("删除失败");
    }

}
